﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Windows.Resources;
using System.Linq;

namespace Rachmans.Sabang
{
    public class QuranHelper
    {
        public Dictionary<int, quranPagesPage> Pages { get { return _pages; } }
        public Dictionary<string, quranSurasSura> Suras { get { return _suras; } }
        public Dictionary<string, quranSura> Quran { get { return _quran; } }
        Dictionary<int, quranPagesPage> _pages = new Dictionary<int, quranPagesPage>();
        Dictionary<string, quranSurasSura> _suras = new Dictionary<string, quranSurasSura>();
        Dictionary<string, quranSura> _quran = new Dictionary<string, quranSura>();

        int MAX_PAGE = 604;
        int MAX_SURA = 114;

        public void LoadPage(int page)
        {
            // Declare an XElement to hold the contents of the xml file. 

            StreamResourceInfo xml = Application.GetResourceStream(new Uri("/Rachmans.Sabang;component/Xml/quran-data.xml", UriKind.Relative));
            XElement appDataXml = XElement.Load(xml.Stream);
            IEnumerable<XElement> searched;

            if (!_pages.ContainsKey(page))
            {
                if (page < MAX_PAGE)
                {
                    //  Get current and next page.
                    searched = from p in appDataXml.Elements("pages").Elements("page")
                               where (int)p.Attribute("index") == page || (int)p.Attribute("index") == page + 1
                               select p;
                }
                else
                {
                    //  Get current and next page.
                    searched = from p in appDataXml.Elements("pages").Elements("page")
                               where (int)p.Attribute("index") == page
                               select p;
                }

                //  Get the value for ayat that need to be loaded.
                foreach (var p in searched)
                {
                    //  Check pages again if page already exist.
                    if (!_pages.ContainsKey((int)p.Attribute("index")))
                    {
                        quranPagesPage quranPage = new quranPagesPage { aya = (string)p.Attribute("aya"), index = (string)p.Attribute("index"), sura = (string)p.Attribute("sura") };
                        _pages.Add((int)p.Attribute("index"), quranPage);
                    }
                }
            }

            int sura1 = Int32.Parse(_pages[page].sura);
            int sura2;

            //  Load Suras.
            if (page < MAX_PAGE)
            {
                //  Load sura in one page after.
                sura2 = Int32.Parse(_pages[page + 1].sura);
            }
            else
            {
                sura2 = MAX_SURA;
            }

            //  Get current sura.
            if (sura1 != sura2)
            {
                searched = from p in appDataXml.Elements("suras").Elements("sura")
                           where (int)p.Attribute("index") >= sura1 && (int)p.Attribute("index") <= sura2
                           select p;
            }
            else
            {
                searched = from p in appDataXml.Elements("suras").Elements("sura")
                           where (int)p.Attribute("index") == sura1
                           select p;
            }

            //  Get the value for ayat that need to be loaded.
            foreach (var p in searched)
            {
                quranSurasSura quranSura = new quranSurasSura { ayas = (string)p.Attribute("ayas"), index = (string)p.Attribute("index") };
                _suras.Add((string)p.Attribute("index"), quranSura);
            }

            //  Load ayas.
            xml = Application.GetResourceStream(new Uri("/Rachmans.Sabang;component/Xml/quran-uthmani.xml", UriKind.Relative));
            appDataXml = XElement.Load(xml.Stream);

            ////  Get current and next page.
            //IEnumerable<XElement> searchedAyas;

            if (page <= MAX_PAGE)
            {
                //  Check sura for requested page and next page. If sura is different then query only requested page.
                if (sura1 != sura2)
                {
                    searched = from s in appDataXml.Elements("sura")
                               where (int)s.Attribute("index") >= sura1 && (int)s.Attribute("index") <= sura2
                               select s;
                }
                else
                {
                    searched = from s in appDataXml.Elements("sura")
                               where (int)s.Attribute("index") == sura1
                               select s;
                }

                foreach (var s in searched)
                {
                    //  Check pages again if page already exist.
                    if (!_quran.ContainsKey((string)s.Attribute("index")))
                    {
                        quranSura sura = new quranSura { name = (string)s.Attribute("name"), index = (string)s.Attribute("index") };
                        List<quranSuraAya> ayas = new List<quranSuraAya>();

                        //  Populate ayas.
                        foreach (var a in s.Elements("aya"))
                        {
                            quranSuraAya aya = new quranSuraAya { bismillah = (string)a.Attribute("bismillah"), index = (string)a.Attribute("index"), text = (string)a.Attribute("text") };
                            ayas.Add(aya);
                        }

                        sura.aya = ayas.ToArray();

                        _quran.Add((string)s.Attribute("index"), sura);
                    }
                }
            }

            //  SHOW the aya here.
        }
    }
}
